package com.practicesuite.controllayer;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import com.practicesuite.systemlayer.Admin;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing a list of all users, which can be selected to be deleted or
 * edited.
 * 
 * @author James
 *
 */
public class UserAdministrationPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    public Patient patient1;
    User currentUser;
    User targetUser;
    String[] userNames;
    private final Admin admin;
    
    /**
     * Create the panel.
     */
    public UserAdministrationPanel(MainWindow container, final List<User> users, final User user) {
        this.window = container;
        admin = (Admin)user;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        userNames = new String[users.size()];
        for (int i = 0; i < users.size(); i++) {
            currentUser = users.get(i);
            userNames[i] = currentUser.getName() + " (" + getUserType(currentUser.getEUID()) + " , ID: " +
            		currentUser.getUserID() + ")";
        }
        
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new GridLayout(0, 1, 0, 0));
        panel_1.setPreferredSize(new Dimension(500, 100));
        
        JLabel lblPatientList = new JLabel(" User List");
        lblPatientList.setHorizontalAlignment(SwingConstants.CENTER);
        panel_1.add(lblPatientList);
        lblPatientList.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);
        panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        
        final JList userList = new JList(userNames);
        userList.setFont(new Font("Tahoma", Font.PLAIN, 12));
        userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        JScrollPane scrollPane = new JScrollPane(userList);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scrollPane);
        
        JButton btnEditPatientInfo = new JButton("Edit User Info");
        btnEditPatientInfo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int userIndex = userList.getSelectedIndex();
                try {
                    targetUser = users.get(userIndex);
                    window.gotoPanel("editUser", targetUser, user);
                } catch (IndexOutOfBoundsException ie) {
                    JOptionPane.showMessageDialog(null, "Please select a user.");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                    e.printStackTrace();
                }
            }
        });
        panel_2.add(btnEditPatientInfo);
        btnEditPatientInfo.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnDeletePatient = new JButton("Delete User");
        btnDeletePatient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int userIndex = userList.getSelectedIndex();
                try {
                    targetUser = users.get(userIndex);
                    SystemDriver.deleteUser(targetUser.getUserID());
                    JOptionPane.showMessageDialog(null, "Patient deleted successfully.");
                    window.gotoPanel("adminHome", admin);
                } catch (IndexOutOfBoundsException ie) {
                    JOptionPane.showMessageDialog(null, "Please select a user.");
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                }
            }
        });
        panel_2.add(btnDeletePatient);
        btnDeletePatient.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnCreatePatientAccount = new JButton("Create User Account");
        btnCreatePatientAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("createUser", admin);
            }
        });
        panel_2.add(btnCreatePatientAccount);
        btnCreatePatientAccount.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JPanel panel_3 = new JPanel();
        panel.add(panel_3, BorderLayout.SOUTH);
        panel_3.setPreferredSize(new Dimension(500, 50));
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("adminHome", admin);
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 15));
        panel_3.add(btnReturnToHome);

    }
    
    private static String getUserType(int eUID) {
        if (eUID == 0) {
            return "Administrator";
        }
        else if (eUID == 1) {
            return "Doctor";
        }
        else if (eUID == 2) {
            return "Nurse";
        }
        else {
            return "Patient";
        }
    }

}
